package swu.cx.myglidetest

import java.security.MessageDigest
class Md5Util {
    companion object{
        fun getMd5Code(inStr: String): String? {
            var md5: MessageDigest? = null
            md5 = try {
                MessageDigest.getInstance("MD5")
            } catch (e: Exception) {
                println(e.toString())
                e.printStackTrace()
                return ""
            }
            val charArray = inStr.toCharArray()
            val byteArray = ByteArray(charArray.size)
            for (i in charArray.indices) byteArray[i] = charArray[i].toByte()
            val md5Bytes: ByteArray = md5!!.digest(byteArray)
            val hexValue = StringBuffer()
            for (i in md5Bytes.indices) {
                val `val` = md5Bytes[i].toInt() and 0xff
                if (`val` < 16) hexValue.append("0")
                hexValue.append(Integer.toHexString(`val`))
            }
            println(hexValue.toString().toUpperCase())
            return hexValue.toString()
        }
    }

}
